package com.neusoft.yuejian_6.activity;

import android.app.Activity;

import android.graphics.Rect;
import android.os.Bundle;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.adapter.ClassListAdapter;
import com.neusoft.yuejian_6.javabean.Classes;
import com.neusoft.yuejian_6.model.ClassModel;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClassActivity extends Activity {
    private ImageView left;
    private ImageView class_pic;
    private TextView class_name;

    private RecyclerView recyclerView;
    private int club_id;

    private LayoutInflater mLayoutInflater;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_class);
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_class);
       // recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        //  new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
     ClassActivity.SpacesItemDecoration decoration=new SpacesItemDecoration(16);
        recyclerView.addItemDecoration(decoration);

    left = (ImageView) findViewById(R.id.left);

    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpacesItemDecoration(int space) {
            this.space=space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    private void initEvent() {
//        //返回上一页
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }


    private void initData()
    {
        Bundle bundle = this.getIntent().getExtras();
        int club_id = bundle.getInt("club_id");
        ClassModel clubModel = new ClassModel();
        Call call = clubModel.getClasses(club_id);
        call.enqueue(new Callback<List<Classes>>() {
            @Override
            public void onResponse(Call<List<Classes>> call, Response<List<Classes>> response) {
                //mDataList= (List) response.body();
                List<Classes> data=response.body();
                System.out.printf("data");
                recyclerView.setAdapter(new ClassListAdapter(ClassActivity.this,data));
            }

            @Override
            public void onFailure(Call<List<Classes>> call, Throwable t) {
              //  Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
